﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace YArchitech.Controls
{
	public class YJKInplaceComboBox : YJKComboBox, IYJKInplaceControl
	{
		public YJKInplaceComboBox()
		{
			this.ParentCell = null;
			base.MinimumSize = new Size(10, 16);
			base.cmbSelectedChanged += this.SelChange;
		}

		public DataGridViewCell ParentCell { get; protected set; }

		public void MoveToCell(DataGridViewCell cell)
		{
			this.ParentCell = cell;
			System.Drawing.Rectangle displayRectangle = cell.GetDisplayRectangle();
			FormUtils.MoveWindow(this, displayRectangle);
			base.Text = cell.GetText();
			base.SelectAll();
			base.Visible = true;
			base.Focus();
		}

		public void OnHide()
		{
			if (this.changeValue)
			{
				int num = 0;
				if (int.TryParse(base.Text, out num) && this.ParentCell != null)
				{
					this.ParentCell.Value = num;
				}
			}
			base.Hide();
			this.ParentCell = null;
		}

		protected override void OnLeave(EventArgs e)
		{
			base.OnLeave(e);
			this.OnHide();
		}

		protected override void OnKeyPress(KeyPressEventArgs e)
		{
			base.OnKeyPress(e);
			int num = Convert.ToInt32(e.KeyChar);
			if (num == 13)
			{
				this.OnHide();
				return;
			}
			if (num != 27)
			{
				this.changeValue = true;
				return;
			}
			this.changeValue = false;
			base.Visible = false;
		}

		private void SelChange(object sender, EventArgs e)
		{
			this.changeValue = true;
		}

		bool IYJKInplaceControl.get_Visible()
		{
			return base.Visible;
		}

		void IYJKInplaceControl.set_Visible(bool value)
		{
			base.Visible = value;
		}

		bool IYJKInplaceControl.Focus()
		{
			return base.Focus();
		}

		private bool changeValue = true;
	}
}
